package com.atbun.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atbun.eduservice.entity.EduSubject;
import com.atbun.eduservice.entity.excel.ExcelSubjectData;
import com.atbun.eduservice.entity.excel.SubjectExcelListener;
import com.atbun.eduservice.mapper.EduSubjectMapper;
import com.atbun.eduservice.service.EduSubjectService;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-05
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    public void importSubjectData(MultipartFile file, EduSubjectService eduSubjectService) {
        try{
//            获取输入流
            InputStream inputStream = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20002,"添加课程分类失败");
        }
    }

    /**
     * 获取所有课程节点
     * @return
     */
    @Override
    public List<EduSubject> queryAllSubjectForList() {
        QueryWrapper<EduSubject> qw = new QueryWrapper<>();
//        查出所有的表数据
        List<EduSubject> subjects = baseMapper.selectList(qw);
        return subjects;
    }
}
